// Copyright (C) 2022 Huawei Device Co., Ltd.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package services

import (
  userdao "dist/dao"
  "dist/model"
  "dist/utils"
  "time"
)

const (
  YEAR   = 1
  MONTH  = 2
  DAY    = 3
  HOUR   = 4
  MINUTE = 5
  SECOND = 6
)

var userMap = make(map[string]string)

func GetUserByUid(uid string) (model.User, error) {
  return userdao.QueryUserByUid(uid)
}

func InsertUser(user model.User) int64 {
  return userdao.InsertUser(user)
}

func InsertAccessRecord(accessRecord model.AccessRecord) (bool, int64) {
  return userdao.InsertUserAccess(accessRecord)
}

func UpdateAccessRecord(userId int64, visitId int64) bool {
  userAccess, error := userdao.QueryUserRecentAccessRecords(userId, visitId)
  if error != nil {
    utils.Error.Printf("%v", error)
    return false
  }
  currentTime := time.Now()
  m, _ := time.ParseDuration("-30m")
  result := currentTime.Add(m)
  if userAccess.AccessTime.Before(result) {
    return userdao.UpdateAccessRecord(userId, userAccess.AccessTime, userAccess.Effective+1, visitId)
  } else {
    return false
  }
}

func GetUserIsExist(uid string) (string, bool) {
  user, exist := userMap[uid]
  if !exist {
    userMap[uid] = uid
  }
  return user, exist
}

func UserVisit(uid string, effectiveAccess bool, visitId int64) (bool, int64) {
  var userId int64 = -1
  user, err := GetUserByUid(uid)
  _, exist := GetUserIsExist(uid)
  if exist && err != nil {
    accessTime := time.Now()
    time.Sleep(1000)
    user, _ := GetUserByUid(uid)
    return InsertAccessRecord(model.AccessRecord{UserId: user.UserId, AccessTime: accessTime})
  }
  if err != nil {
    userId = InsertUser(model.User{Uid: uid, CreateTime: time.Now()})
  } else {
    userId = user.UserId
  }
  if userId != -1 {
    if effectiveAccess {
      return UpdateAccessRecord(userId, visitId), -1
    } else {
      return InsertAccessRecord(model.AccessRecord{UserId: userId, AccessTime: time.Now()})
    }
  } else {
    return false, -1
  }
}

func getIntervalStr(interCode int) string {
  var interval string
  switch interCode {
  case YEAR:
    interval = "YYYY"
    break
  case MONTH:
    interval = "YYYY-MM"
    break
  case DAY:
    interval = "YYYY-MM-DD"
    break
  case HOUR:
    interval = "YYYY-MM-DD HH24"
    break
  case MINUTE:
    interval = "YYYY-MM-DD HH24:MI"
    break
  default:
    interval = ""
    break
  }
  return interval
}

func GetHomePageVisitCollection() []interface{} {
  return userdao.QueryHomePageVisit()
}

func GetHomePageUserCollection() []interface{} {
  return userdao.QueryHomePageUser()
}

func GetHomePageChar(startTime time.Time, endTime time.Time, interCode int) map[string][]model.HomePageChar {
  interval := getIntervalStr(interCode)
  return userdao.QueryHomePageChar(startTime, endTime, interval)
}
